import com.sun.security.auth.callback.TextCallbackHandler;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

public class JaasAcn {

    public static void main(String[] args) {

        String path = "/root/jaas_test/";

        System.setProperty("java.security.auth.login.config", path + "jaas.conf");

        System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");

        // sun.security.krb5.debug
        System.setProperty("sun.security.krb5.debug", "true");

        LoginContext lc = null;
        try {
            lc = new LoginContext("JaasSample", new TextCallbackHandler());

            // attempt authentication
            try {
                lc.login();
            } catch (LoginException le) {
                le.printStackTrace();
                System.err.println("Authentication failed:");
                System.err.println("  " + le.getMessage());
                System.exit(-1);
            }

        } catch (LoginException le) {
            System.err.println("Cannot create LoginContext. " + le.getMessage());

        } catch (SecurityException se) {
            System.err.println("Cannot create LoginContext. " + se.getMessage());
            System.exit(-1);
        }

        System.out.println("Authentication succeeded!");

    }
}
